﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using KOF.Models;
using Newtonsoft.Json;
using KOF.Helpers;
using System.Collections.ObjectModel;
using System.IO.IsolatedStorage;
using System.Linq;


namespace KOF.ViewModels
{
    public class BrandViewModel : ViewModelBase
    {
        public BrandViewModel()
        {
            //ListItemSource = GetAllBrands();
            GetAllBrands();
        }
     
        public void GetAllBrands()
        {
            WebClient wc = new WebClient();
            wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
            wc.DownloadStringAsync(new Uri("http://app.kicksonfire.com/kofapp/index.php/api/events/GetEventTypes"));
        }

        void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                //List<ServiceResult> deserialized = JsonConvert.DeserializeObject<List<ServiceResult>>(e.Result);
                //var deserialized = JsonConvert.DeserializeObject<ServiceResult>(e.Result);
                ObservableCollection<ResponseBrand> lstBrnds = null;
                if (IsolatedStorageSettings.ApplicationSettings.Contains("SelectedBrands"))
                {
                    lstBrnds = (ObservableCollection<ResponseBrand>)IsolatedStorageSettings.ApplicationSettings["SelectedBrands"];
                }

                var deserialized = JsonConvert.DeserializeObject<ServiceResult>(e.Result);
                if (ListItemSource == null) ListItemSource = new ObservableCollection<ResponseBrand>();
                foreach (KOF.Helpers.ResponseBrand a in deserialized.types)
                {
                    if (lstBrnds != null && lstBrnds.Count > 0)
                    {
                        var v = (from b in lstBrnds
                                 where b.brand_name == a.brand_name
                                 select b).SingleOrDefault();
                        if (v != null)
                        {
                            a.checked1 = true;
                        }
                    }
                    ListItemSource.Add(a);
                }
            }
            catch (Exception ex)
            {
                
            }
        }
        private ObservableCollection<KOF.Helpers.ResponseBrand> _listItemSource;
        public ObservableCollection<KOF.Helpers.ResponseBrand> ListItemSource
        {
            get
            {
                //if (_listItemSource == null)
                //    _listItemSource = new List<Release>();
                return _listItemSource;
            }
            set
            {
                if (_listItemSource != value)
                {
                    _listItemSource = value;
                }
                OnPropertyChanged("ListItemSource");
            }
        }

    }
   
    }

